package com.cn.Stream;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamTest {
    /**
     * 传统方法：找出所有长度大于5的水果，并转为大写
     */
    @Test
    public void test1(){
        //传统方法：找出所有长度大于5的水果，并转为大写
        String[] fruitString = {"apple", "banana", "cherry", "date", "elderberry"};
        List<String> fruits1 = Arrays.asList(fruitString);
        List<String> fruits2 = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");
        List<String> longFruitsUpperCase = new ArrayList<>();
        for (String fruit : fruits2) {
            if(fruit.length() > 5){
                longFruitsUpperCase.add(fruit.toUpperCase());
            }
        }
        System.out.println(longFruitsUpperCase);
    }

    /**
     * Stream方式：找出所有长度大于5的水果，并转为大写
     */
    @Test
    public void test2(){
        //Stream方式
        List<String> fruits = Arrays.asList("apple", "banana", "orange", "grape", "kiwifruit");
        List<String> longFruitsUpperCaseStream = fruits.stream().filter(fruit -> fruit.length() > 5).map(String::toUpperCase).collect(Collectors.toList());
        System.out.println(longFruitsUpperCaseStream);
    }
}

